﻿using MediatR;
using Mysqlx.Prepare;
using ProductionManagement.Application.Command.ProOrderCommand;
using ProductionManagement.Domain.Dtos;
using ProductionManagement.Infrstructre.lnterface;

namespace ProductionManagement.Application.CommandHandler.ProOrderCommandHandlers
{
    public class FillProOrderCommandHnadlers : IRequestHandler<FillProOrderCommand, FillProOrderDTO>
    {


        private readonly IProOrderRepository _proOrder;

        public readonly IProOrderDetailRepository _proOrderDetail;

        private readonly IProductionPlanRepository _productionPlan;
        private readonly IProductionProductRepository _productionProduct;

        public FillProOrderCommandHnadlers(IProOrderRepository proOrder, IProOrderDetailRepository proOrderDetail, IProductionPlanRepository productionPlan, IProductionProductRepository productionProduct)
        {
            _proOrder = proOrder;
            _proOrderDetail = proOrderDetail;
            _productionPlan = productionPlan;
            _productionProduct = productionProduct;

        }

        public async Task<FillProOrderDTO> Handle(FillProOrderCommand request, CancellationToken cancellationToken)
        {
            var orders = (from a in (await _proOrder.GetAll()) select a).ToList();

            var details = (from a in (await _proOrderDetail.GetAll()) select a).ToList();

            var plans = (from a in (await _productionPlan.GetAll()) select a).ToList();

            var products = (from a in (await _productionProduct.GetAll()) select a).ToList();

            var list = (from a in orders
                        where a.POId == request.POid
                        select new FillProOrderDTO
                        {
                            POId = a.POId,
                            POrderNumber = a.POrderNumber,
                            PDocumentDate = a.PDocumentDate,
                            PDocumentMakers = a.PDocumentMakers,
                            POrderTopic = a.POrderTopic,
                            PPlanName = a.PPlanName,
                            PRemark = a.PRemark,
                            TotalData = a.TotalData,
                            OrderDetails = (from b in details
                                            join c in plans on b.ProductionPlansId equals c.ProductionPlansId
                                            join d in products on c.PPDocumentNumber equals d.PPDocumentNumber
                                            where a.POrderNumber == b.POId
                                            select new FilltOrderDetailDTO
                                            {
                                                POId = b.POId,
                                                ProductionPlansId = b.ProductionPlansId,
                                                PCompletionDate = b.PCompletionDate,
                                                PDeliveryDate = b.PDeliveryDate,
                                                POrderNum = b.POrderNum,
                                                PPId = b.PPId,
                                                PPlanName = c.PPlanName,
                                                ProductionType = b.ProductionType,
                                                PStartTime = b.PStartTime,
                                                PName = d.PName,
                                                PNumber = d.PNumber,
                                                PPNum = d.PPNum,
                                                MSubject = d.MSubject,
                                                PPAppEndDate = d.PPAppEndDate,
                                                PPAppStartDate = d.PPAppStartDate,
                                                PPReleDate = d.PPReleDate,
                                                ProductId = d.ProductId,
                                                PSpec = d.PSpec,
                                                PUnit = d.PUnit,
                                                 PPTotalQuantity = c.PPTotalQuantity,


                                            }).ToList(),

                        }).FirstOrDefault();

            return list;
            

        }
    }
}
